<?php

namespace app\admin\controller;

use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use think\Controller;
use think\Db;
use think\db\Query;
use think\Request;
use cmf\controller\AdminBaseController;
use app\admin\model\StudentModel;


class StudentController extends AdminBaseController
{
    public function index()
    {
        $name=$this->request->param('name');
        $school_id = $this->request->param('school_id');
        $room = Db::name('room')
            ->where('school_id', $school_id)
            ->select();
        $room_id = input('room_id');
        $school=Db::name('school')->select();
        $student=Db::name('student')
        ->join('cmf_room','cmf_student.room_id=cmf_room.id','LEFT')
        ->join('cmf_school','cmf_room.school_id=cmf_school.id','LEFT')
        ->join('cmf_record','cmf_record.student_id=cmf_student.id','LEFT')
        ->join('cmf_score','cmf_score.id=cmf_record.score_id','LEFT')
        ->field('cmf_student.*,cmf_school.name as school_name,cmf_room.name as room_name,cmf_school.id as school_id')
            ->field('sum(cmf_score.name) as total')
            ->group('cmf_student.id')
            ->where(function(Query $query) use ($name,$school_id,$room_id){
                if($name){
                    $query->where('cmf_student.name','like',"%$name%");
                }
                if ($school_id) {
                    $query->where('cmf_school.id', 'eq', $school_id );
                }
                if ($room_id) {
                    $query->where('cmf_room.id', 'eq', $room_id);
                }
            })
            ->order("id DESC")
            ->paginate(3, false, ['query' => request()->param()]);
        $page=$student->render();
        return view('index',['student'=>$student,
            'page'=>$page,
            'school_id'=>$school_id,
            'room_id'=>$room_id,
            'school'=>$school,
            'room'=>$room]);
    }
    public function add()
    {
        $school=SchoolModel::select();
        $room=RoomModel::select();
        return $this->fetch('',['school'=>$school,'room'=>$room]);
    }

    public function addPost()
    {
        $data=input();
        $data['create_time']=date('Y-m-d H:i:s',time());
        unset($data['photo_name']);
        unset($data['school_id']);
        $add=Db::name('student')->insert($data);
        if ($add){
            $this->success('添加成功','student/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function getRoomBySchool(){
        $schoolId=input('school_id');
        $rooms=RoomModel::where('school_id',$schoolId)->select();
        if ($rooms){
            $options=[];
            foreach ($rooms as $v){
                $opt="<option value='".$v->id."'>".$v->name."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }
    public function getAdd(){
        $schoolId=input('school_id');
        $rooms=Db::name('room')
            ->where('school_id',$schoolId)
            ->select();
        if ($rooms){
            $option=["<option value=''>请选择班级</option>"];
            foreach ($rooms as $val){
                $opt="<option value='".$val['id']."'>".$val['name']."</option>";
                array_push($option,$opt);
            }
            return $option;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }
    public function edit()
    {
        $id=input('id');
        $student=StudentModel::get($id);
        $school=SchoolModel::all();
        $schoolId=RoomModel::where('id',$student["room_id"])->value('school_id');
        $room=RoomModel::where('school_id',$schoolId)->select();
        return $this->fetch('',['school'=>$school,
            'room'=>$room,
            'student'=>$student,
            'schoolId'=>$schoolId,
            'school_id'=>$schoolId
        ]);
    }

    public function editPost()
    {
        $data=input();
        if (empty($data['name'])){
            $this->error('姓名不能为空~');
        }
        $data['create_time']=date('Y-m-d H:i:s',time());
        unset($data['photo_name']);
        unset($data['school_id']);
        $edit=StudentModel::update($data,['id'=>$data['id']]);
        if ($edit){
            $this->success('修改成功','student/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete($id)
    {
        $data=input();
        $del=StudentModel::destroy($data['id']);
        if ($del){
            $this->success('删除成功！','student/index');
        }else{
            $this->error('删除失败');
        }
    }
    public function delAll(){
        $ids=input('ids/a');
        $del=Db::name('student')->delete($ids);
        if ($del){
            $this->success('删除成功！','student/index');
        }else{
            $this->error('删除失败');
        }
    }

}
